import { IFeidaoAiBrowserComponent } from '@dfeidao/atom-web/interfaces';
import get from '@dfeidao/atom-web/local/get';
import nodejs from '@dfeidao/atom-web/msg/nodejs';
import refresh from '@dfeidao/atom-web/url/refresh';

export default async function a003(fd: IFeidaoAiBrowserComponent) {// 修改的保存
	// todo
	console.log('zj-000005:a002----start');
	// 获取隐患源代码
	const hc = fd.data.node.querySelector<HTMLInputElement>('[data-id="hidden_code1"]').value;
	if (!hc) {
		alert('请完善信息');
		return;
	}

	// 获取隐患源名称
	const hn = fd.data.node.querySelector<HTMLInputElement>('[data-id="hidden_name1"]').value;
	if (!hn) {
		alert('请完善信息');
		return;
	}

	// 获取隐患源评价级别下拉框
	const assess_level = fd.data.node.querySelector<HTMLSelectElement>('[data-id="assess_level1"]');
	const assess = assess_level.value;
	console.log('隐患源评价级别', assess);
	if (assess === '请选择') {
		alert('请完善信息');
		return;
	}

	// 获取隐患源管控层级下拉框
	const control_grade = fd.data.node.querySelector<HTMLSelectElement>('[data-id="control_grade1"]');
	const grade = control_grade.value;
	console.log('隐患源管控层级', grade);
	if (grade === '请选择') {
		alert('请完善信息');
		return;
	}

	// 获取隐患源评价级别下拉框
	const control_level = fd.data.node.querySelector<HTMLSelectElement>('[data-id="control_level1"]');
	const level = control_level.value;
	console.log('隐患源评价级别', level);
	if (level === '请选择') {
		alert('请完善信息');
		return;
	}

	// 获取负责部门下拉框
	const department = fd.data.node.querySelector<HTMLSelectElement>('[data-id="duty_department1"]');
	const departmentno = department.value;
	console.log('负责部门', grade);
	let departmentname = "";
	if (departmentno === '请选择') {
		alert('请完善信息');
		return;
	} else {
		departmentname = department.selectedOptions[0].text;
	}

	// 获取负责人下拉框
	const staff = fd.data.node.querySelector<HTMLSelectElement>('[data-id="responsible1"]');
	const staffno = staff.value;
	console.log('负责人', staffno);
	let staffname = "";
	if (staffno === '请选择') {
		alert('请完善信息');
		return;
	} else {
		staffname = staff.selectedOptions[0].text;
	}
	// 获取工程技术措施文本域
	const engineer = fd.data.node.querySelector<HTMLTextAreaElement>('[data-id="engineer_skill1"]').value;
	if (!engineer) {
		alert('请完善信息');
		return;
	}

	// 获取个体防护文本域
	const personal = fd.data.node.querySelector<HTMLTextAreaElement>('[data-id="personal_protect1"]').value;
	if (!personal) {
		alert('请完善信息');
		return;
	}

	// 获取培训教育文本域
	const train = fd.data.node.querySelector<HTMLTextAreaElement>('[data-id="train_education1"]').value;
	if (!train) {
		alert('请完善信息');
		return;
	}

	// 获取管理措施文本域
	const manage = fd.data.node.querySelector<HTMLTextAreaElement>('[data-id="manage_measure1"]').value;
	if (!manage) {
		alert('请完善信息');
		return;
	}

	// 获取应急处置措施文本域
	const emergency = fd.data.node.querySelector<HTMLTextAreaElement>('[data-id="emergency_measures1"]').value;
	if (!emergency) {
		alert('请完善信息');
		return;
	}

	// 隐患源代码查重

	const no = get(fd, 'no') as string;
	const res = await nodejs<Array<{ hidden_source: string; }>>('wyf-hManager/zj-000004/s001', { hidden_code: hc });
	console.log(res);
	let flag = true;
	if (res.length > 0) {
		for (let i = 0; i < res.length; i++) {
			if (res[i].hidden_source !== no) {
				flag = false;
			}
		}
	}
	if (!flag) {
		alert('特性代码已存在');
	} else {
		const res1 = await nodejs<{ code: number; result: string }>('wyf-hManager/zj-000005/s002', {
			hidden_source: no,
			filter: {
				duty_department_no: departmentno,
				duty_department: departmentname,
				responsible: staffname,
				responsible_task_no: staffno,
				hidden_code: hc,
				hidden_name: hn,
				control_level: level,
				control_grade: grade,
				assess_level: assess,
				engineer_skill: engineer,
				manage_measure: manage,
				train_education: train,
				emergency_measures: emergency,
				personal_protect: personal
			}
		});
		if (res1.code === 1) {
			alert('保存成功');
			refresh();
		} else {
			alert('保存失败');
		}

	}

	console.log('zj-000005:a002----end');
}
